package org.phoenixframework.api.search.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.phoenixframework.api.search.service.IMemberReadHistoryService;
import org.phoenixframework.common.entity.ums.MemberReadHistory;
import org.phoenixframework.common.tools.ResultModel;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author doublelifeke
 * Email: hautxxxyzjk@163.com
 * DateTime: 2021/7/25 13:55
 * Description:
 */
@Slf4j
@Api(tags = {"会员商品浏览记录管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/memberReadHistory")
public class MemberReadHistoryController {

    private final IMemberReadHistoryService memberReadHistoryService;

    @ApiOperation("创建浏览记录")
    @PostMapping("/create")
    public ResultModel<?> create(@RequestBody MemberReadHistory memberReadHistory) {
        int count = memberReadHistoryService.create(memberReadHistory);
        if (count > 0) {
            return ResultModel.ok();
        } else {
            return ResultModel.error();
        }
    }

    @ApiOperation("删除浏览记录")
    @DeleteMapping("/delete")
    public ResultModel<?> delete(@RequestParam("ids") List<String> ids) {
        int count = memberReadHistoryService.delete(ids);
        if (count > 0) {
            return ResultModel.ok();
        } else {
            return ResultModel.error();
        }
    }

    @ApiOperation("清空除浏览记录")
    @PostMapping("/clear")
    public ResultModel<?> clear() {
        memberReadHistoryService.clear();
        return ResultModel.ok();
    }

    @ApiOperation("分页获取用户浏览记录")
    @GetMapping("/list/{current}/{size}")
    public ResultModel<?> list(@ApiParam(name = "current", value = "当前页码", defaultValue = "0")
                               @PathVariable int current,
                               @ApiParam(name = "size", value = "每页记录数", defaultValue = "10")
                               @PathVariable int size) {
        PageRequest pageRequest = PageRequest.of(current, size);
        Page<MemberReadHistory> page = memberReadHistoryService.list(pageRequest);
        return ResultModel.ok(page);
    }
}
